package t844

import (
	"errors"
	"sync"
)

type MyStack struct {
	Items []rune
	l     sync.RWMutex
}

func (m *MyStack) Push(s rune) {
	m.l.Lock()
	defer m.l.Unlock()
	m.Items = append(m.Items, s)
}

func (m *MyStack) Pop() (rune, error) {
	m.l.Lock()
	defer m.l.Unlock()
	if len(m.Items) == 0 {
		return 0, errors.New("empty of stack")
	}
	el := m.Items[len(m.Items)-1]
	m.Items = m.Items[:len(m.Items)-1]
	return el, nil
}

func (m *MyStack) Top() (rune, error) {
	m.l.Lock()
	defer m.l.Unlock()
	if len(m.Items) == 0 {
		return 0, errors.New("empty of stack")
	}
	el := m.Items[len(m.Items)-1]
	return el, nil
}
